﻿using FishSimulation.Model;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Controls;
using Timer = System.Timers.Timer;

namespace FishSimulation.Views
{
    /// <summary>
    /// Interaction logic for LegendView.xaml
    /// </summary>
    public partial class LegendView : UserControl
    {
        #region Members
        Timer timer = new Timer(1000);
        TaskScheduler uiContext = null;
        #endregion

        #region Constructors
        public LegendView()
        {
            InitializeComponent();
            uiContext = TaskScheduler.FromCurrentSynchronizationContext();

            timer.Enabled = true;
            timer.Elapsed += timer_Elapsed;
        }
        #endregion

        #region Event Handlers
        private void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            Task.Factory.StartNew(() => { this.OnElapsed(); }, CancellationToken.None, TaskCreationOptions.None, uiContext);
        }
        #endregion

        #region Methods
        private void OnElapsed() {
            lblTicks.Content = "Ticks: {0}".Formatt(Repository.Instance.World.Ticks);
            dg.ItemsSource = Legend.Create();
        }
        #endregion
    }
}
